package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC71 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String simplifyPath(String path) {
            List<String> shortPath = new ArrayList<>();
            String[] pathList = path.split("/");
            for (String cur : pathList) {
                if (cur.length() <= 0 || " ".equals(cur) || ".".equals(cur)) continue;
                if ("..".equals(cur)) {
                    if (shortPath.size() > 0) shortPath.remove(shortPath.size() - 1);
                } else shortPath.add(cur);
            }

            StringBuilder finalPath = new StringBuilder();
            for (String tempPath : shortPath) {
                finalPath.append("/").append(tempPath);
            }

            return finalPath.length() <= 0 ? "/" : finalPath.toString();
        }
    }

    public void run() {
        Solution solution = new Solution();
        String path = "/../";//  /c
        System.out.println(solution.simplifyPath(path));
    }

    public static void main(String[] args) throws Exception {
        LC71 an = new LC71();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
